home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 7: Sunsite
/
Linux Cubed Series 7 - Sunsite Vol 1.iso
/
system
/
filesyst
/
dosfs
/
dmsdosfs.000
/
dmsdosfs
/
dmsdosfs-0.6.9b
/
ddefrag.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-20
|
4KB
|
177 lines
/*
ddefrag.c
DMSDOS filesystem: defragment utility
The DMSDOS filesystem was written by Frank Gockel
(gockel@etecs4.uni-duisburg.de).
COPYRIGHT (C) 1995,1996 Frank Gockel
Distributed under the GNU General Public License.
*/
#include<stdio.h>
#include<linux/dmsdos_fs.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
typedef struct
{ int clusternr;
int startsector;
} Clist;
Clist clist[65535];
int printed;
void init_prozent()
{ printed=0;
printf("[0%% 50%% 100%%]\n[");
}
void prozent(int akt,int end)
{ double grmpf;
int rund;
int i;
grmpf=53.0*((double)akt)/((double)end);
rund=grmpf+0.5;
if(rund>printed)
{ for(i=0;i<rund-printed;++i)printf(".");
printed=rund;
}
}
void exit_prozent()
{ printf("]\n");
}
void error(int err)
{ if(err==-EPERM)printf("Permission denied.\n");
else if(err==-EIO)printf("I/O Error.\n");
else if(err==-EINVAL)printf("Invalid parameter.\n");
else printf("Error %d occured.\n",err);
}
int comparfn(Clist*el1,Clist*el2)
{ return el1->startsector-el2->startsector;
}
void do_defrag(int fd,int max_cluster)
{ int i,ret;
Mdfat_entry mde;
unsigned long w[10];
printf("Checking filesystem...\n");
ioctl(fd,DMSDOS_SIMPLE_CHECK,w);
if(w[0]==1||w[0]==2)printf("Check aborted due to lack of kernel memory.\n");
if(w[0]==-1)printf("Filesystem has serious errors: FAT level crosslink(s) found.\n");
if(w[0]==-2)printf("Filesystem has serious errors: MDFAT level crosslink(s) found.\n");
if(w[0]==-3)printf("Filesystem has minor errors: BITFAT mismatches MDFAT.\n");
if(w[0]!=0)
{ printf("Didn't pass filesystem check, program aborted.\n");
return;
}
clist[0].clusternr=0;clist[0].startsector=0;
clist[1].clusternr=1;clist[1].startsector=0;
printf("Getting info...\n");
/* get info */
init_prozent();
for(i=2;i<=max_cluster;++i)
{ prozent(i-2,max_cluster-2);
clist[i].clusternr=i;
w[0]=i;
w[1]=&mde;
ret=ioctl(fd,DMSDOS_READ_MDFAT,w);
if(ret<0){error(ret);return;}
clist[i].startsector=mde.sector_minus_1+1;
}
exit_prozent();
printf("Sorting...\n");
qsort(&(clist[2]),max_cluster-1,sizeof(Clist),comparfn);
/*
printf("Sorted list:\n");
for(i=2;i<max_cluster;++i)
{ printf("Clusternr: %5d Startsector: %9d\n",
clist[i].clusternr,clist[i].startsector);
}
printf("Not defragmenting.\n");
return;
*/
printf("Consolidating free space...\n");
init_prozent();
for(i=2;i<=max_cluster;++i)
{ prozent(i-2,max_cluster-2);
ioctl(fd,DMSDOS_MOVEBACK,clist[i].clusternr);
}
exit_prozent();
printf("Done.\n");
}
void main(int argc, char*argv[])
{ Dblsb dblsb;
int fd;
int ret;
if(argc!=2)
{
printf("DMSDOS defrag utility (C) 1996 Frank Gockel\n");
printf("Usage: %s (directory)\n",argv[0]);
return;
}
fd=open(argv[1],O_RDONLY);
if(fd<0)
{ printf("No such file or directory.\n");
return;
}
/* this hack enables reverse version check */
dblsb.s_dcluster=DMSDOS_VERSION;
ret=ioctl(fd,DMSDOS_GET_DBLSB,&dblsb);
if(ret<0)
{ printf("This is not a DMSDOS directory.\n");
close(fd);
return;
}
printf("You are running DMSDOS Version %d.%d.%d.\n\n",(ret&0xff0000)>>16,
(ret&0x00ff00)>>8,ret&0xff);
if(ret&0x0f000000)
{ printf("Sorry, this program is too old for the actual dmsdos version.\n");
close(fd);
return;
}
if(ret<0x00000608)
{ printf("This program requires at least DMSDOS Version 0.6.8.\n");
close(fd);
return;
}
if(dblsb.s_comp==READ_ONLY)
{ printf("Sorry, filesystem is mounted read-only.\n");
close(fd);
return;
}
do_defrag(fd,dblsb.s_max_cluster);
close(fd);
}